// pages/stored_value_card/index.js
var app = getApp();
var rootDocment = app.globalData.postUrl;
import {
  throttle
} from '../../utils/common.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rootDocment: rootDocment,
    change_succeed: false,
    userName: '',
    passWord: '',
    CardInfoData: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options, 'onLoad')
    let that = this
    if (options.q && options.q != "undefined") {
      const qrUrl = decodeURIComponent(options.q)
      console.log(qrUrl, 'qrUrl');
      //此处就是我们要获取的参数 json，通过方法解析
      let jsonUrl = that.GetwxUrlParam(qrUrl);
      console.log(jsonUrl, 'jsonUrl');
      //比如我要得到id的值，直接取值即可
      let id = jsonUrl.id;
      //let id = 123456;
      // if (id) {
      //   that.getCardInfo(id)
      //   that.setData({
      //     userName: id
      //   })
      // }
      // lese {

      // }

    }

  },
  getCardInfo(id) {
    var that = this;
    var paraArr = new Array();
    paraArr['id'] = id;

    var sign = app.signature(paraArr);
    wx.request({
      url: rootDocment + '/api/chuzhi_card/index',
      data: {
        id: paraArr['id'],
        sign: sign
      },
      method: 'GET',
      header: {},
      success: function (res) {
        // wx.hideLoading()
        console.log('卡信息', res.data.data);
        if (res.data.data.is_active == 1) {
          wx.showToast({
            title: '此卡已兑换',
            icon: 'none'
          })
        }
        that.setData({
          CardInfoData: res.data.data
        });
      }
    })

  },
  verify: throttle(function () { //验证榴莲卡密码
    let that = this
    app.login(function (userInfo) {
      that.setData({
        userInfo: userInfo
      });
    });
    if (!that.data.userInfo || !that.data.userInfo.userID) {
      return
    }
    let data = this.data
    let pwd = data.passWord
    let id = data.userName
    let user_id = that.data.userInfo.userID
    var paraArr = new Array();
    paraArr['id'] = id;
    paraArr['pwd'] = pwd;
    paraArr['user_id'] = user_id;
    var sign = app.signature(paraArr);
    if (id == "") {
      wx.showToast({
        title: '未识别到卡号，请重新扫码',
        icon: 'none'
      })
      return
    }
    if (pwd == "") {
      wx.showToast({
        title: '请输入密码',
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '兑换中',
      mask: true
    })
    wx.request({
      url: rootDocment + '/api/chuzhi_card/activation',
      data: {
        id: id,
        pwd: pwd,
        user_id: user_id,
        sign: sign
      },
      method: 'POST',
      header: {},
      success: function (res) {
        wx.hideLoading()
        if (res.data.code == 1001) {
          // wx.showToast({
          //   title: '激活成功',
          //   icon: 'none'
          // })
          // setTimeout(() => {
          //   app.gotaburl('user/index')
          // }, 2000)
          that.setData({
            change_succeed: true
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
        //console.log(res,'==============>getMiniappConfig')

      },
      fail: function (err) {
        wx.hideLoading()
      }
    })
  }),
  passWordChange: function (e) { //输入框双向绑定
    this.setData({
      [e.target.dataset.id]: e.detail.value
    })
  },
  GetwxUrlParam(url) {
    let theRequest = {};
    if (url.indexOf("#") != -1) {
      const str = url.split("#")[1];
      const strs = str.split("&");
      for (let i = 0; i < strs.length; i++) {
        theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
      }
    } else if (url.indexOf("?") != -1) {
      const str = url.split("?")[1];
      const strs = str.split("&");
      for (let i = 0; i < strs.length; i++) {
        theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
      }
    }
    return theRequest;
  },
  on_close() {
    this.setData({
      change_succeed: false
    })
    app.gotaburl('user/index')
  },
  on_sure() {
    this.setData({
      change_succeed: false
    })
    app.gotaburl('user/index')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})